# -*- coding: utf-8 -*-
import scrapy
from scrapy import Request
from urllib.parse import urlencode
import json
from mySpider.items import ImageItem


class ImageSpider(scrapy.Spider):
    name = 'image'
    allowed_domains = ['image.so.com']
    start_urls = ['http://image.so.com/']

    def start_requests(self):
        data = {'ch': 'photography', 'listtype': 'new', 'temp': 1}
        for page in range(1,self.settings.get('MAX_PAGE')+1):
            data['sn'] = page*30
            params = urlencode(data)
            yield Request('http://image.so.com/zjl?'+params, self.parse)

    def parse(self, response):
        result = json.loads(response.text)
        for image in result.get('list'):
            item = ImageItem()
            item['id'] = image.get('id')
            item['url'] = image.get('qhimg_url')
            item['title'] = image.get('title')
            item['desc'] = image.get('pic_desc')
            item['site_name'] = image.get('sitename')
            yield item
